package com.gxa.community.admin.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gxa.community.admin.mapper.OperateKillSpellMapper;
import com.gxa.community.admin.service.GroupService;
import com.gxa.community.admin.service.OperateKillSpellService;
import com.gxa.community.core.pojo.domain.OperateKillSpell;
import com.gxa.community.core.pojo.dto.SeckillQueryDto;
import com.gxa.community.core.pojo.vo.EditkillVo;
import com.gxa.community.core.pojo.vo.SeckillAddVo;
import com.gxa.community.core.utils.CodingUtils;
import com.gxa.community.core.utils.R;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/group")
public class GroupController {
    @Autowired
    private OperateKillSpellService secKillService;
    @Autowired
    private GroupService groupService;
    @Autowired
    private OperateKillSpellMapper operateKillSpellMapper;

    @GetMapping("/groups")
    public R QueryAllgroups(){
        R r = secKillService.SelectAllSecKill(2);
        return r;
    }

    @GetMapping("/preaddgroup")
    public R preaddGroup(){
        String s = CodingUtils.RandomNum();
        return R.ok().put("data",s);
    }

    @PostMapping("/addgroup")
    public R addGroup(@RequestBody SeckillAddVo seckillAddVo){
        R r = groupService.AddGroup(seckillAddVo,2);
        return r;
    }

    @DeleteMapping("/delgroup")
    public R delGroup(@NotNull String id){
        R r = secKillService.delSekill(id);
        return r;
    }

    @PutMapping("/editgroup")
    public R editGroup(@RequestBody EditkillVo editkillVo){
        R r = secKillService.editSeckill(editkillVo,2);
        return r;
    }

    @PutMapping("/editgroupstatus")
    public R editGroupStatus(@NotNull String killId){
        UpdateWrapper<OperateKillSpell> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("kill_id",killId);
        OperateKillSpell operateKillSpell = new OperateKillSpell();
        operateKillSpell.setStatus(1);
        int update = operateKillSpellMapper.update(operateKillSpell, updateWrapper);
        if (update!=0){
            return R.ok("修改成功");
        }
        return R.error("修改失败");
    }

    @GetMapping("/preeditgroup")
    public R preEditGroup(@NotNull String killId){
        R r = secKillService.preEditSeckill(killId);
        return r;
    }

    @GetMapping("/querygroputext")
    public R queryGroupText(String name,
                            @Length(min = -1, max = 2) Integer activeStatus,
                            @Length(min = 0, max = 2)Integer status,
                            Long startTime,Long endTime){
        R r = secKillService.queryText(name, activeStatus, status, startTime, endTime,2);
        return r;
    }

    /**
     * 前台远程通信接口
     * 获取所有的拼团活动
     * @return List<SeckillQueryDto> 包含了所有的商品信息
     */
    @GetMapping("/gropuQueryList")
    public List<SeckillQueryDto> gropuQueryList(){
        return secKillService.gropuQueryList();
    }
}
